<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.storeName" placeholder="请输入店铺名称"
                            prop="storeName"></globalSearchTemplate>
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.status" placeholder="店铺状态"
                            prop="status" type="select"></globalSearchTemplate>
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.businessId" placeholder="所属商户"
                            prop="businessId" type="select" :load="businessRemoteDataUrl"></globalSearchTemplate>
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.storeCategoryId" placeholder="店铺分类"
                            prop="storeCategoryId" type="cas" :load="categoryCasDataUrl"
                            :props="{emitPath:false,checkStrictly: true}"></globalSearchTemplate>
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.startTime" type="date"
                            placeholder="开始时间"></globalSearchTemplate>
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.endTime" type="date"
                            placeholder="结束时间"></globalSearchTemplate>
      <el-button class="filter-item" v-waves @click="handleSearch" icon="el-icon-search">搜索</el-button>
      <el-button class="filter-item" v-if="storeManager_btn_add" v-waves @click="handleCreate" type="primary"
                 icon="edit">添加
      </el-button>
    </div>
    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit
              highlight-current-row style="width: 100%">
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="编号" prop="serialNumber" width="170"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="店铺名称" prop="storeName" width="130"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="所属商户" prop="businessId" width="150" type="select"
                         :load="businessRemoteDataUrl"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="LOGO" prop="logo" type="img" width="100"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="所属分类" prop="storeCategoryId" width="170" type="cas"
                         :load="categoryCasDataUrl"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="保证金" prop="bailPaid" width="120"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="到期日期" prop="endDate" width="160"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="店铺状态" prop="status" width="100" type="select"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="创建日期" prop="createDate" width="170"></globalTableColumn>
      <el-table-column label="操作" width="170">
        <template slot-scope="scope">
          <el-button v-if="storeManager_btn_upd" size="small" type="success"
                     @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button v-if="storeManager_btn_del" size="mini" type="danger"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.page"
                     :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="35%">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <globalFormItem :key="formKey" :config="globalConfig" label="所属商户" v-model="form.businessId" prop="businessId"
                        placeholder="请选择商户" type="select" :load="businessRemoteDataUrl"></globalFormItem>

        <globalFormItem :key="formKey" :config="globalConfig" label="店铺名称" v-model="form.storeName" prop="storeName"
                        placeholder="请输入店铺名称"></globalFormItem>

        <el-form-item label="店铺标签:" prop="keywords">
          <el-button v-if="!mKeywordVisible" class="button-new-keyword" type="primary" @click="showInput">+ 设置
          </el-button>
          <el-input v-else ref="mKeywordInput" v-model="mKeyword" class="input-new-keyword"
                    @keyup.enter.native="handleInputConfirm"/>
          <el-tag v-for="item in form.keywords" :key="item" closable type="primary"
                  @close="handleClose(item)"
                  v-if="item== '' ? false : true">
            {{'#' +item }}
          </el-tag>
        </el-form-item>
        <globalFormItem :key="formKey" :config="globalConfig" label="店铺分类" v-model="form.storeCategoryId"
                        prop="storeCategoryId" placeholder="请输入店铺分类" type="cas" :load="categoryCasDataUrl"
                        :props="{emitPath:false,checkStrictly: true}"></globalFormItem>
        <globalFormItem :key="formKey" :config="globalConfig" label="LOGO" v-model="form.logo" prop="logo"
                        type="img" width="100"></globalFormItem>
        <globalFormItem :key="formKey" :config="globalConfig" label="保证金" v-model="form.bailPaid" prop="bailPaid"
                        placeholder="请输入保证金"></globalFormItem>
        <globalFormItem :key="formKey" :config="globalConfig" label="到期日期" v-model="form.endDate" prop="endDate"
                        placeholder="请输入到期日期"></globalFormItem>
        <globalFormItem :key="formKey" :config="globalConfig" label="店铺状态" v-model="form.status" prop="status"
                        placeholder="请选择状态"></globalFormItem>
        <globalFormItem :key="formKey" :config="globalConfig" label="e-mail" v-model="form.email" prop="email"
                        placeholder="请输入e-mail"></globalFormItem>
        <el-form-item label="店铺地址" prop="address" style="width: 420px">
          <el-input v-model="form.address" :rows="6" type="textarea" placeholder="请输入店铺地址"/>
        </el-form-item>
        <el-form-item label="店铺简介" prop="introduction" style="width: 420px">
          <el-input v-model="form.introduction" :rows="6" type="textarea" placeholder="请输入简介"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
        <el-button v-else type="primary" @click="update('form')">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped>
  .filter-item {
    margin-left: 10px;
  }

  .el-tag + .el-tag {
    margin-left: 10px;
  }

  .input-new-keyword {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }

</style>

<script>
  import { fetchList, getObj, addObj, putObj, delObj, casObj, remoteData } from '@/api/store/store'
  import waves from '@/directive/waves/index.js' // 水波纹指令
  import { mapGetters } from 'vuex'
  import { globalNotify } from '@/util/notifyUtils'  //弹窗
  import { getToken } from '@/util/auth'
  import globalSearchTemplate from '@/components/global-el-search-template'
  import globalTableColumn from '@/components/global-el-table-column'
  import globalFormItem from '@/components/global-el-form-item'
  import { remoteData as categoryRemoteData, casObj as categoryCasData } from '@/api/store/storeCategory'
  import { remoteData as businessRemoteData } from '@/api/store/business'

  export default {
    name: 'store',
    components: { globalSearchTemplate, globalTableColumn, globalFormItem },
    directives: {
      waves
    },
    data() {
      return {
        mKeyword: undefined,
        mKeywordVisible: false,
        categoryCasDataUrl: categoryCasData,
        categoryRemoteDataUrl: categoryRemoteData,
        businessRemoteDataUrl: businessRemoteData,
        globalConfig: {
          module: ['store'],
          componentName: ['store']
        },
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
          'startTime': undefined,
          'endTime': undefined,
          'address': undefined,
          'bailPaid': undefined,
          'email': undefined,
          'endDate': undefined,
          'introduction': undefined,
          'keywords': undefined,
          'mobile': undefined,
          'storeName': undefined,
          'status': undefined,
          'businessId': undefined,
          'storeCategoryId': undefined
        },
        rules: {},
        form: {
          'address': undefined,
          'bailPaid': undefined,
          'email': undefined,
          'endDate': undefined,
          'introduction': undefined,
          'keywords': [],
          'logo': undefined,
          'mobile': undefined,
          'storeName': undefined,
          'status': undefined,
          'businessId': undefined,
          'storeCategoryId': undefined
        },
        dialogFormVisible: false,
        dialogStatus: '',
        storeManager_btn_add: false,
        storeManager_btn_upd: false,
        storeManager_btn_del: false,
        textMap: {
          update: '编辑',
          create: '创建'
        },
        tableKey: 0,
        headers: {
          Authorization: 'Bearer ' + getToken()
        },
        casDataUrl: casObj,
        remoteDataUrl: remoteData,
        key: '',
        formKey: ''
      }
    },
    computed: {
      ...mapGetters([
        'permissions'
      ])
    },
    filters: {},
    created() {
      this.getList()
      this.storeManager_btn_add = this.permissions['storeManager_btn_add']
      this.storeManager_btn_upd = this.permissions['storeManager_btn_upd']
      this.storeManager_btn_del = this.permissions['storeManager_btn_del']
    },
    methods: {
      handleClose(val) {
        this.form.keywords.splice(this.goodsForm.keywords.indexOf(val), 1)
      },
      handleInputConfirm() {
        const keyword = this.mKeyword
        if (keyword != '' && keyword != undefined) {
          this.form.keywords.push(keyword)
        }
        this.mKeywordVisible = false
        this.mKeyword = undefined
      },
      showInput() {
        this.mKeywordVisible = true
        this.$nextTick(_ => {
          this.$refs.mKeywordInput.$refs.input.focus()
        })
      },
      getList() {
        this.listLoading = true
        fetchList(this.listQuery).then(response => {
            this.list = response.data.data.records
            this.refreshTable()
            this.total = response.data.data.total
            this.listLoading = false
          }
        )
      },
      handleSizeChange(val) {
        this.listQuery.limit = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getList()
      },
      handleDelete(row) {
        delObj(row.id)
          .then(response => {
              this.dialogFormVisible = false
              this.getList()
              globalNotify(this, response)
            }
          )
      },
      handleCreate() {
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.resetForm()
        this.refreshForm()
      },
      refreshTable() {
        this.key = new Date().getTime()
      },
      refreshForm() {
        this.formKey = new Date().getTime()
      },
      handleUpdate(row) {
        this.resetForm()
        getObj(row.id).then(response => {
          this.form = response.data.data
          this.refreshForm()
          this.dialogFormVisible = true
          this.dialogStatus = 'update'
        })

      },
      create(formName) {
        const set = this.$refs
        set[formName].validate(valid => {
            if (valid) {
              addObj(this.form)
                .then((response) => {
                    this.dialogFormVisible = false
                    this.getList()
                    globalNotify(this, response)
                  }
                )
            } else {
              return false
            }
          }
        )
      },
      cancel(formName) {
        this.resetForm()
        this.dialogFormVisible = false
      },
      update(formName) {
        const set = this.$refs
        set[formName].validate(valid => {
            if (valid) {
              this.dialogFormVisible = false
              this.form.password = undefined
              putObj(this.form).then((response) => {
                  this.dialogFormVisible = false
                  this.getList()
                  globalNotify(this, response)
                }
              )
            } else {
              return false
            }
          }
        )
      },
      resetForm() {
        this.form = {
          'address': undefined,
          'bailPaid': undefined,
          'email': undefined,
          'endDate': undefined,
          'introduction': undefined,
          'keywords': [],
          'logo': undefined,
          'mobile': undefined,
          'storeName': undefined,
          'status': undefined,
          'businessId': undefined,
          'storeCategoryId': undefined
        }
      },
      handleSearch() {
        this.listQuery.page = 1
        this.getList()
      }
    }
  }
</script>
